package com.idp.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.idp.app.dto.AppVersionDto;
import com.idp.app.dto.AppVersionSearchDto;
import com.idp.app.entity.AppVersionEntity;
import com.idp.app.mapper.AppVersionMapper;
import com.idp.app.service.AppVersionService;
import com.idp.common.enums.MessageEnum;
import com.idp.common.error.SystemException;
import com.idp.common.model.PageModel;
import com.idp.common.utils.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 版本维护Service实现类
 * @author King
 */
@Service("appVersionService")
public class AppVersionServiceImpl implements AppVersionService {

	@Autowired
	private AppVersionMapper appVersionMapper;

	@Override
	public PageModel<AppVersionDto> findByPage(AppVersionSearchDto appVersionSearchDto) {

		PageModel<AppVersionDto> page = new PageModel<AppVersionDto>();

		if(appVersionSearchDto.getReleaseTimeEnd() != null) {
			appVersionSearchDto.setReleaseTimeEnd(DateUtil.endOfDay(appVersionSearchDto.getReleaseTimeEnd()));
		}

		page.setList(appVersionMapper.getList(appVersionSearchDto));

		return page;
	}

	@Override
	public AppVersionDto getById(String id) {

		if(ObjectUtil.isEmpty(id)) {

			throw new SystemException(MessageEnum.PARAM_ERROR);
		}

		AppVersionDto appVersionDto = appVersionMapper.getById(id);

		if(appVersionDto == null) {

			throw new SystemException(MessageEnum.DATA_NOT_EXIST);
		}

		return appVersionDto;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void add(AppVersionDto appVersionDto) {

		AppVersionEntity appVersionEntity = BeanUtil.toBean(appVersionDto, AppVersionEntity.class);

		appVersionEntity.setCreateUser(ContextUtil.getLoginUserId());
		appVersionEntity.setCreateTime(new Date());

		appVersionMapper.insert(appVersionEntity);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void update(AppVersionDto appVersionDto) {

		AppVersionEntity appVersionEntity = appVersionMapper.selectById(appVersionDto.getId());

		if(appVersionEntity == null) {
			throw new SystemException(MessageEnum.DATA_NOT_EXIST);
		}

		BeanUtil.copyProperties(appVersionDto, appVersionEntity);

		appVersionEntity.setUpdateUser(ContextUtil.getLoginUserId());
		appVersionEntity.setUpdateTime(new Date());

		appVersionMapper.updateById(appVersionEntity);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delete(String id) {

		AppVersionEntity appVersionEntity = appVersionMapper.selectById(id);

		if(appVersionEntity == null) {
			throw new SystemException(MessageEnum.DATA_NOT_EXIST);
		}

		appVersionMapper.deleteById(id);
	}

	@Override
	public AppVersionDto checkVersion(Integer versionCode, String appType) {

		AppVersionDto version = appVersionMapper.getLastVersion(versionCode, appType);

		if(version != null) {

			return version;
		}

		return null;
	}

}
